import { login as loginRequest } from '@/services/user';
import { removeToken, setToken } from '@/utils/auth';

export async function login(body: API.LoginParams) {
  return loginRequest(body).then(res => {
    if (res.code === 200) {
      setUserInfo(res.data)
      setToken(res.data.accessToken)
    }
    return res
  });
}

export async function outLogin() {
  localStorage.removeItem('xmbz-user-info')
  removeToken();
  return true
}

export const setUserInfo = (data: COMTYPE.UserInfo) => {
  localStorage.setItem('xmbz-user-info', JSON.stringify(data))
}

export const getUserInfo = async () => {
  const userInfoStr = localStorage.getItem('xmbz-user-info')
  return JSON.parse(userInfoStr || 'null')
}
